import moment from "moment";

Date.prototype.Format = function(fmt)
{
  var o = {
    "M+" : this.getMonth()+1,                 //月份
    "d+" : this.getDate(),                    //日
    "h+" : this.getHours(),                   //小时
    "m+" : this.getMinutes(),                 //分
    "s+" : this.getSeconds(),                 //秒
    "q+" : Math.floor((this.getMonth()+3)/3), //季度
    "S"  : this.getMilliseconds()             //毫秒
  };
  if(/(y+)/.test(fmt))
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)
    if(new RegExp("("+ k +")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  return fmt;
};

export function timestampToENDate(timestamp) {
    if(timestamp){
      let newDate = new Date(timestamp);
      return newDate.toLocaleDateString();
    }
    return '';
}

export function timestampToENDateCopy(timestamp) {
    if(timestamp){
        let newDate = new Date(timestamp);
        let dateString = newDate.toLocaleDateString();
        let date_array = dateString.split('/');
        if(date_array && date_array[1] && date_array[1].length === 1){
            date_array[1] = '0' + date_array[1];
        }
        if(date_array && date_array[2] &&date_array[2].length === 1){
            date_array[2] = '0' + date_array[2];
        }
        dateString = date_array[0] + '/' +  date_array[1] + '/' +  date_array[2];
        return dateString;
    }
    return '';
}

export function timestampToENDateFormatFull(timestamp) {
  if (timestamp) {
    var newDate = new Date(timestamp);
    let dateString = newDate.toLocaleDateString();
    let date_array = dateString.split('/');
    if (date_array && date_array[1] && date_array[1].length == 1) {
      date_array[1] = '0' + date_array[1];
    }
    if (date_array && date_array[2] && date_array[2].length == 1) {
      date_array[2] = '0' + date_array[2];
    }
    dateString = date_array[0] + '-' + date_array[1] + '-' + date_array[2];
    let time = newDate.toTimeString();
    let index_ = time.indexOf(' ');
    let allTimeString = time.substr(0, index_);
    return dateString + ' ' +allTimeString;
  }
  return '';
}

export function timestampToFullDate(timestamp) {
    if(timestamp){
        let newDate = new Date(timestamp);
        let dateString = newDate.toLocaleDateString();
        let time = newDate.toTimeString();
        let index_ = time.indexOf(' ');
        let allTimeString = time.substr(0,index_);
        return dateString+' '+allTimeString;
    }
    return '';
}

export function datestampToChinaString(datestamp) {
    if(typeof datestamp !== 'string'){
        let date_string = new Date(datestamp).toLocaleDateString();
        let date_array = date_string.split('/');
        if(date_array[1] && date_array[1].length<2){
            date_array[1] = '0'+ date_array[1];
        }
        if(date_array[2] && date_array[2].length<2){
            date_array[2] = '0'+ date_array[2];
        }
        date_string = date_array[0]+'年'+date_array[1]+'月'+date_array[2]+'日';
        return date_string;
    }
    return datestamp;
}

export const CN_LOCALE = {
  "lang": {
    "placeholder": "选择录制时间范围",
    "rangePlaceholder": ["开始时间", "截止时间"],
    "today": "今天",
    "now": "现在",
    "backToToday": "返回今天",
    "ok": "确认",
    "clear": "清理",
    "month": "月",
    "year": "年",
    "timeSelect": "选择小时",
    "dateSelect": "选择日期",
    "monthSelect": "请选择月份",
    "yearSelect": "请选择年份",
    "decadeSelect": "Choose a decade",
    "yearFormat": "YYYY",
    "dateFormat": "YYYY/M/D",
    "dayFormat": "D",
    "dateTimeFormat": "YYYY-MM-DD HH:mm:ss",
    "monthFormat": "MMMM",
    "monthBeforeYear": true,
    "previousMonth": "前一个月 (PageUp)",
    "nextMonth": "下一个月 (PageDown)",
    "previousYear": "去年 (Control + left)",
    "nextYear": "明年 (Control + right)",
    "previousDecade": "Last decade",
    "nextDecade": "Next decade",
    "previousCentury": "Last century",
    "nextCentury": "Next century"
  },
  "timePickerLocale": {
    "placeholder": "选择时间"
  }
};

//moment获取以往时间
export function getRecentCustomTime(value,time,unit) {
  let startTime,endTime;
  if(unit == "HOUR"){
    startTime = moment(time).subtract(value, "hours").format("YYYY-MM-DD HH:mm:ss");
    endTime = moment(time).format("YYYY-MM-DD HH:mm:ss"); //当前时间
  }else if(unit == "DAY"){
    startTime = moment(time).subtract(value, "days").format("YYYY-MM-DD");
    endTime = moment(time).format("YYYY-MM-DD"); //当前时间
  }else if(unit == "MONTH"){
    startTime = moment(time).subtract(value, "months").format("YYYY-MM");
    endTime = moment(time).format("YYYY-MM"); //当前时间
  }else if(unit == "YEAR"){
    startTime = moment(time).subtract(value, "years").format("YYYY");
    endTime = moment(time).format("YYYY"); //当前时间
  }
  return [startTime,endTime]
}

export function getRecentOneDayTime() {
  let date = new Date();
  const endTime = date.Format("yyyy-MM-dd hh:mm:ss");
  date.setDate(date.getDate() - 1);
  const beginTime = date.Format("yyyy-MM-dd hh:mm:ss");
  return [beginTime,endTime]
}

export function getRecentTwoDayTime() {
  let date = new Date();
  const endTime = date.Format("yyyy-MM-dd hh:mm:ss");
  date.setDate(date.getDate() - 2);
  const beginTime = date.Format("yyyy-MM-dd hh:mm:ss");
  return [beginTime,endTime]
}

export function getTodayStartAndEndTime() {
  let date = new Date();
  const beginTime = new Date(new Date(date.toLocaleDateString()).getTime()).Format("yyyy-MM-dd hh:mm:ss");
  const endTime = new Date(new Date(date.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1).Format("yyyy-MM-dd hh:mm:ss");
  return [beginTime,endTime]
}

export function getYesterdayStartAndEndTime() {
  let date = new Date();
  date.setDate(date.getDate() - 1);
  const beginTime = new Date(new Date(date.toLocaleDateString()).getTime()).Format("yyyy-MM-dd hh:mm:ss");
  const endTime = new Date(new Date(date.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1).Format("yyyy-MM-dd hh:mm:ss");
  return [beginTime,endTime]
}

export function getThisWeekStartAndEndTime() {
  let date = new Date();
  let nowDayOfWeek = date.getDay(); //今天本周的第几天
  nowDayOfWeek = nowDayOfWeek ? nowDayOfWeek : 7;
  let nowDay = date.getDate(); //当前日
  let nowMonth = date.getMonth(); //当前月
  let nowYear = date.getFullYear(); //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0; //
  const beginTimeTemp = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  const endTimeTemp = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  //周日-周一-----》周一-周日
  beginTimeTemp.setDate(beginTimeTemp.getDate() + 1);
  endTimeTemp.setDate(endTimeTemp.getDate() + 1);
  const beginTime = beginTimeTemp.Format("yyyy-MM-dd hh:mm:ss");
  const endTime =new Date(new Date(endTimeTemp.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1).Format("yyyy-MM-dd hh:mm:ss");
  return [beginTime,endTime]
}

export function getLastWeekStartAndEndTime() {
  let date = new Date();
  date.setDate(date.getDate() - 7);
  let nowDayOfWeek = date.getDay(); //今天本周的第几天
  nowDayOfWeek = nowDayOfWeek ? nowDayOfWeek : 7;
  let nowDay = date.getDate(); //当前日
  let nowMonth = date.getMonth(); //当前月
  let nowYear = date.getFullYear(); //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0; //
  const beginTimeTemp = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  const endTimeTemp = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  //周日-周一-----》周一-周日
  beginTimeTemp.setDate(beginTimeTemp.getDate() + 1);
  endTimeTemp.setDate(endTimeTemp.getDate() + 1);
  const beginTime = beginTimeTemp.Format("yyyy-MM-dd hh:mm:ss");
  const endTime =new Date(new Date(endTimeTemp.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1).Format("yyyy-MM-dd hh:mm:ss");
  return [beginTime,endTime]
}

export function getThisMonthStartAndEndTime() {
  let date = new Date();
  let nowMonth = date.getMonth(); //当前月
  let nowYear = date.getFullYear(); //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0; //
  const monthStartDate = new Date(nowYear, nowMonth, 1);
  const monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowYear,nowMonth));
  const beginTime = monthStartDate.Format("yyyy-MM-dd hh:mm:ss");
  const endTime =new Date(new Date(monthEndDate.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1).Format("yyyy-MM-dd hh:mm:ss");
  return [beginTime,endTime]
}

export function getLastMonthStartAndEndTime() {
  let date = new Date();
  let nowYear = date.getFullYear(); //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0; //

  let lastMonthDate = new Date(); //上月日期
  lastMonthDate.setDate(1);
  lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
  let lastMonth = lastMonthDate.getMonth();

  const lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  const lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(nowYear,lastMonth));

  const beginTime = lastMonthStartDate.Format("yyyy-MM-dd hh:mm:ss");
  const endTime =new Date(new Date(lastMonthEndDate.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1).Format("yyyy-MM-dd hh:mm:ss");
  return [beginTime,endTime]
}

function getMonthDays(nowYear,myMonth){
  let monthStartDate = new Date(nowYear, myMonth, 1);
  let monthEndDate = new Date(nowYear, myMonth + 1, 1);
  let days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
  return days;
}

export function getCustomTime(value){
  let statPeriod = "";
  let beginTime = "";
  let endTime = "";
  let period = "";

  switch (value){
    case "5分钟内" : statPeriod = "5MINUTE";break;
    case "10分钟内" : statPeriod = "10MINUTE";break;
    case "20分钟内" : statPeriod = "20MINUTE";break;
    case "30分钟内" : statPeriod = "30MINUTE";break;
    case "1小时内" : statPeriod = "1HOUR";break;
    case "2小时内" : statPeriod = "2HOUR";break;
    case "6小时内" : statPeriod = "6HOUR";break;
    case "1天内" :
      beginTime = getRecentOneDayTime()[0];
      endTime = getRecentOneDayTime()[1];
      period = "HOUR";
      break;
    case "2天内" :
      beginTime = getRecentTwoDayTime()[0];
      endTime = getRecentTwoDayTime()[1];
      period = "HOUR";
      break;
    case "今天" :
      beginTime = getTodayStartAndEndTime()[0];
      endTime = getTodayStartAndEndTime()[1];
      period = "DAY";
      break;
    case "昨天" :
      beginTime = getYesterdayStartAndEndTime()[0];
      endTime = getYesterdayStartAndEndTime()[1];
      period = "DAY";
      break;
    case "本周" :
      beginTime = getThisWeekStartAndEndTime()[0];
      endTime = getThisWeekStartAndEndTime()[1];
      period = "DAY";
      break;
    case "上周" :
      beginTime = getLastWeekStartAndEndTime()[0];
      endTime = getLastWeekStartAndEndTime()[1];
      period = "DAY";
      break;
    case "本月" :
      beginTime = getThisMonthStartAndEndTime()[0];
      endTime = getThisMonthStartAndEndTime()[1];
      period = "MONTH";
      break;
    case "上月" :
      beginTime = getLastMonthStartAndEndTime()[0];
      endTime = getLastMonthStartAndEndTime()[1];
      period = "MONTH";
      break;
    default : break;
  }

  return {statPeriod,beginTime,endTime,period}
}

export function getMinuteStartAndEndTime(value) {
  let date=new Date();     //1. js获取当前时间
  const beginTime = date.Format("yyyy-MM-dd hh:mm:ss");
  let min=date.getMinutes();  //2. 获取当前分钟
  date.setMinutes(min-value);  //3. 设置当前时间+n分钟：把当前分钟数+n后的值重新设置为date对象的分钟数
  let y = date.getFullYear();
  let m = (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date.getMonth() + 1);
  let d = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate();
  let h = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours()
  let f = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes()
  let s = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds()
  let endTime = y+'-'+m+'-'+d + " " + h + ":" + f + ":" + s;
  return [endTime,beginTime];
}
export function getTimeAndPeriod(value) {
  let period = "";
  let beginTime = "";
  let endTime = "";

  switch (value){
    case "5MINUTE" :
      beginTime = getMinuteStartAndEndTime(5)[0];
      endTime = getMinuteStartAndEndTime(5)[1];
      period = 'HOUR'
      break;
    case "10MINUTE" :
      beginTime = getMinuteStartAndEndTime(10)[0];
      endTime = getMinuteStartAndEndTime(10)[1];
      period = 'HOUR'
      break;
    case "20MINUTE" :
      beginTime = getMinuteStartAndEndTime(20)[0];
      endTime = getMinuteStartAndEndTime(20)[1];
      period = 'HOUR'
      break;
    case "30MINUTE" :
      beginTime = getMinuteStartAndEndTime(30)[0];
      endTime = getMinuteStartAndEndTime(30)[1];
      period = 'HOUR'
      break;
    case "1HOUR" :
      beginTime = getMinuteStartAndEndTime(60)[0];
      endTime = getMinuteStartAndEndTime(60)[1];
      period = 'HOUR'
      break;
    case "2HOUR" :
      beginTime = getMinuteStartAndEndTime(120)[0];
      endTime = getMinuteStartAndEndTime(120)[1];
      period = 'HOUR'
      break;
    case "6HOUR" :
      beginTime = getMinuteStartAndEndTime(360)[0];
      endTime = getMinuteStartAndEndTime(360)[1];
      period = 'HOUR'
      break;
    default : break;
  }
  return {period,beginTime,endTime}
}
